/*
author: qi.tong
date: 2016.05.19
*/

// eslint-disable no-sequences
jQuery(document).ready(function($) {
    // 百度统计
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement('script');
        hm.src = '//hm.baidu.com/hm.js?ad0624cf51ddee8ef3bd2513c2add79b';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(hm, s);
    })();
});

/**
 * sentry-sdk
 * 可以容错但会丢失脚本加载之前的错误  (sentryErrorCachedQueue 上传sdk加载成功之前缓存的错误)
 */
(function() {
    var location = window.location;
    function isDev() {
        return location.hostname.indexOf('www.wenjuan') === -1 && location.port === '';
    }
    function installSentry() {
        var sentry = document.createElement('script');
        sentry.src = '//s1.wenjuan.com/lib/tracker/browser/1.0.2/tracker.js?' + Date.now();
        var s = document.getElementsByTagName('script')[0];
        sentry.async = 1;
        s.parentNode.insertBefore(sentry, s);
        function analysisModuleName(url) {
            if (/\/s\//i.test(url)) {
                return 'answer';
            }
            return 'main';
        }
        sentry.onload = function() {
            window.TrackerBrowser.setup({
                dsn: 'https://7a90ff19459d4701a6bc9118e6ca3f80@sentry.idiaoyan.cn/6',
                release: 'v2.0.0', // 暂时固定
                wjTags: {
                    service_name: analysisModuleName(location.href)
                }
            });
        };
    }
    // 暂时生产不引入
    try {
        isDev() && installSentry();
    } catch (e) {
        console.log('install sentry error', e);
    }
})();

// track-sdk
// prettier-ignore
/* eslint-disable-next-line */
(function(i,s,o,g,r,a,m){if(i[r])return;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments);},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m);})(window,document,'script','https://s1.wenjuan.com/lib/tracksdk/monitor/track-dc-sdk.589f0d0.js','_track');

// 多端统一埋点sdk
// prettier-ignore
/* eslint-disable-next-line */
(function(i,s,o,g,r,a,m){if(i[r])return;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments);},i[r].l=1*new Date(); a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g; m.parentNode.insertBefore(a, m);})(window, document, 'script', '//s1.wenjuan.com/lib/track-sdk/@wenjuan/track-sdk-javascript@1.0.7/track.js', 'zytrack');
window.zytrack &&
    window.zytrack('config', {
        timingUpload: true,
        interval: 100
    });

// 将cookie字符串转成对象格式
function analyzeCookie(totalCookie) {
    if (!totalCookie) {
        return;
    }
    var cookieArr = totalCookie.split(';');
    var cookieObject = {};
    for (var i = 0; i < cookieArr.length; i++) {
        var cookieItem = cookieArr[i].split('=');
        cookieObject[cookieItem[0].trim()] = cookieItem[1].trim();
    }
    return cookieObject;
}
// 删除冗余的cookie，str是要删除的cookie字符串（会做自动匹配） iskeepOne是否保留重复中的一条
function deleteRedundancyCookieByRegexp(str, isKeepOne) {
    var totalCookieObj = analyzeCookie(document.cookie);
    var re = new RegExp('\\w*' + str + '\\w*', 'i'); // 正则匹配前后字符
    var firstMatch = '';
    var deleteNum = 0;
    for (var k in totalCookieObj) {
        if (re.test(k)) {
            if (!firstMatch && isKeepOne) {
                // 保留第一条匹配项
                firstMatch = k;
            } else {
                deleteNum++;
                window.delCookie && window.delCookie(k);
            }
        }
    }
    // console.log('共删除匹配 ' + re + ' 项: ' + deleteNum + '个')
    // console.log('保留第一条匹配项: ' + firstMatch);
}
// 删除冗余的 *_gr_session_id_* 标识的cookie
deleteRedundancyCookieByRegexp('_gr_session_id_');
